<template>
  <div>
<!--    <div style="margin-top: 12px">-->
<!--      <el-input style="width: 200px" placeholder="请输入文件名称" suffix-icon="el-icon-search" v-model="name"></el-input>-->
<!--      <el-input style="width: 200px" placeholder="请输入文件大小" suffix-icon="el-icon-user"  class="ml-5" v-model="size"></el-input>-->
<!--      <el-input style="width: 200px" placeholder="请输入昵称" suffix-icon="el-icon-message"  class="ml-5" v-model="email"></el-input>-->
<!--      <el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position"  class="ml-5" v-model="address"></el-input>-->
<!--      <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>-->
<!--      <el-button style="margin-left: 5px" type="warning" @click="reset">重置</el-button>-->
<!--    </div>-->
<!--    <div style="margin: 6px 0">-->
<!--      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>-->
<!--      <el-popconfirm title="确认批量删除吗?" style="margin: 0 10px" @confirm="delBatch">-->
<!--        <template #reference>-->
<!--          <el-button type="danger" >批量删除<i class="el-icon-remove-outline"></i></el-button>-->
<!--        </template>-->
<!--      </el-popconfirm>-->
<!--      <el-upload action="http://localhost:9090/file/upload" :show-file-list="false"-->
<!--                 :on-success="handleFileUploadSuccess"-->
<!--                 style="display: inline-block">-->
<!--        <el-button type="primary">上传文件<i class="el-icon-top"></i></el-button>-->
<!--      </el-upload>-->
<!--    </div>-->
<!--    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55"></el-table-column>-->
<!--      <el-table-column prop="id" label="id"></el-table-column>-->
<!--      <el-table-column prop="name" label="文件名称" width="186"></el-table-column>-->
<!--      <el-table-column prop="type" label="文件类型"></el-table-column>-->
<!--      <el-table-column prop="size" label="文件大小（kb）"></el-table-column>-->
<!--      <el-table-column prop="likes" label="点赞数" ></el-table-column>-->
<!--      <el-table-column label="下载" >-->
<!--        <template slot-scope="scope">-->
<!--          <el-button size="small"  type="text" @click="download(scope.row.url)">下载</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="启用链接" width="70px">-->
<!--        <template slot-scope="scope">-->
<!--          <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ccc"-->
<!--                     @change="changeEnable(scope.row)"-->
<!--          ></el-switch>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--          fixed="right"-->
<!--          label="操作"-->
<!--          width="100">-->
<!--        <template #default = "scope">-->
<!--          <el-button size="small" type="text" @click="handleEdit(scope.row)"  style="padding: 2px">编辑</el-button>-->
<!--          <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)">-->
<!--            <template #reference>-->
<!--              <el-button size="small"  type="text" style="padding: 2px">删除</el-button>-->
<!--            </template>-->
<!--          </el-popconfirm>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->
<!--    <div style="padding: 2px 0">-->
<!--      <el-pagination-->
<!--          @size-change="handleSizeChange"-->
<!--          @current-change="handleCurrentChange"-->
<!--          :current-page="pageNum"-->
<!--          :page-sizes="[2, 4, 6, 8]"-->
<!--          :page-size="pageSize"-->
<!--          layout="total, sizes, prev, pager, next, jumper"-->
<!--          :total="total">-->
<!--      </el-pagination>-->
<!--    </div>-->
<!--    <el-dialog title="文件信息" :visible.sync="dialogFormVisible" width="30%">-->
<!--      <el-form :model="form" label-width="60px">-->
<!--        <el-form-item label="名称">-->
<!--          <el-input v-model="form.name" autocomplete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="类型">-->
<!--          <el-input v-model="form.type" autocomplete="off"></el-input>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click="dialogFormVisible = false; form = []">取 消</el-button>-->
<!--        <el-button type="primary" @click="save">确 定</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
    <h1>新闻管理</h1>
  </div>
</template>

<script>
export default {
  name: "News",
  data() {
    return {
      tableData: [],
      total:0,
      name:"",
      size:"",
      nickname:"",
      email:"",
      address:"",
      pageSize:4,
      pageNum:1,
      form:{},
      dialogFormVisible: false,
      multipleSelection: [],
    }
  },
  created() {
    this.load();
  },
  methods :{
    // 请求分页查询数据
    load(){
      this.request.get("file/page",{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          size: this.size,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    // 保存
    save() {
      this.request.post("/file/update", this.form).then(res =>{
        if(res.data) {
          this.$message.success("保存成功")
        } else {
          this.$message.error("保存失败")
        }
        this.load()
      })
      this.dialogFormVisible = false;
      this.form = [];
    },
    handleFileUploadSuccess() {
      this.load();
    },
    // 删除
    handleDelete(id){
      this.request.delete("/file/delete/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
        this.load()
      })
    },
    //实现批量选择
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    //批量删除
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id) // 得到每个选择对象的 id
      console.log(ids)
      this.request.post("/file/del/batch", ids).then(res => {
        if (res.data) {
          this.$message.success("批量删除成功")
        } else {
          this.$message.error("批量删除失败")
        }
        this.load()
      })
    },
    // 改变页大小
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load();
    },
    // 改变当前页
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load();
    },
    // 重新设置搜索框
    reset() {
      this.name = ""
      this.size = ""
      this.email = ""
      this.address = ""
      this.load()
    },
    // 打开新增表单
    handleAdd() {
      this.form = {}
      this.dialogFormVisible = true

    },
    download(url) {
      window.open(url)
    },
    changeEnable(row) {
      this.request.post("/file/update", row).then(res =>{
        if(res.code === '200') {
          this.$message.success("更新成功")
        } else {
          this.$message.error("更新失败")
        }
      })
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    }

  }
}
</script>